#include "widget.h"
#include "ui_widget.h"
#include<QPushButton>
#include<QIcon>
#include<QRect>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    QIcon icon(":/left.png");
    ui->pushButton_left->setIcon(icon);

    QIcon icon1(":/right.png");
    ui->pushButton_right->setIcon(icon1);


    QIcon icon2(":/up.png");
    ui->pushButton_up->setIcon(icon2);

    QIcon icon3(":/down.png");
    ui->pushButton_down->setIcon(icon3);

    QIcon icon4(":/a.jpg");
    ui->pushButton_target->setIconSize(QSize(100,100));
    ui->pushButton_target->setIcon(icon4);

    //直接通过按键的名字设置
//    ui->pushButton_up->setShortcut(QKeySequence("w"));
//    ui->pushButton_down->setShortcut(QKeySequence("s"));
//    ui->pushButton_left->setShortcut(QKeySequence("a"));
//    ui->pushButton_right->setShortcut(QKeySequence("d"));


    //通过按键的枚举设置
//    ui->pushButton_up->setShortcut(QKeySequence(Qt::CTRL+Qt::Key_W));//ctrl+w
//    ui->pushButton_down->setShortcut(QKeySequence(Qt::Key_S));
//    ui->pushButton_left->setShortcut(QKeySequence(Qt::Key_A));
//    ui->pushButton_right->setShortcut(QKeySequence(Qt::Key_D));


    //按键默认就是连发的  鼠标点击的不是
    ui->pushButton_up->setAutoRepeat(true);
    ui->pushButton_down->setAutoRepeat(true);
    ui->pushButton_left->setAutoRepeat(true);
    ui->pushButton_right->setAutoRepeat(true);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_left_clicked()
{
    QRect ret =  ui->pushButton_target->geometry();

    ui->pushButton_target->setGeometry(ret.x()-5,ret.y(),ret.width(),ret.height());


}

void Widget::on_pushButton_down_clicked()
{
    QRect ret = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(ret.x(),ret.y()+5,ret.width(),ret.height());
}

void Widget::on_pushButton_right_clicked()
{
    QRect ret = ui->pushButton_target->geometry();
    ui->pushButton_target->setGeometry(ret.x()+5,ret.y(),ret.width(),ret.height());
}

void Widget::on_pushButton_up_clicked()
{
       QRect ret = ui->pushButton_target->geometry();
       ui->pushButton_target->setGeometry(ret.x(),ret.y()-5,ret.width(),ret.height());
}

